﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PlcWriteDbInfoController : ControllerBase
    {
        private readonly ILogUtil _logger;
        private readonly PlcWriteDbInfoApp _PlcWriteDbInfoApp;
        public PlcWriteDbInfoController(ILogUtil logger, PlcWriteDbInfoApp PlcWriteDbInfoApp)
        {
            _logger = logger;
            _PlcWriteDbInfoApp = PlcWriteDbInfoApp;
        }

        [HttpPost]
        public async Task<HeaderResult<long>> AddPlcWriteDbInfoAsync(PlcWriteDbInfoDto PlcWriteDbInfoDto)
        {
            try
            {
                long result = await _PlcWriteDbInfoApp.AddPlcWriteDbInfoAsync(PlcWriteDbInfoDto);
                return ApiBase.Success(result, "新增PlcWriteDb配置成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<long>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<bool>> DeletePlcWriteDbInfoAsync(long ID)
        {
            try
            {
                bool result = await _PlcWriteDbInfoApp.DeletePlcWriteDbInfoAsync(ID);
                return ApiBase.Success<bool>("删除PlcWriteDb配置成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> UpdatePlcWriteDbInfoAsync(PlcWriteDbInfoDto PlcWriteDbInfoDto)
        {
            try
            {
                bool result = await _PlcWriteDbInfoApp.UpdatePlcWriteDbInfoAsync(PlcWriteDbInfoDto);
                return ApiBase.Success<bool>("修改PlcWriteDb配置成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<List<PlcWriteDbInfoDto>>> GetAllPlcWriteDbInfoAsync(QueryPlcWriteDbInfoDto queryPlcWriteDbInfoDto)
        {
            try
            {
                var pageList = await _PlcWriteDbInfoApp.GetPlcWriteDbInfoAsync(queryPlcWriteDbInfoDto);
                return ApiBase.Info(pageList.Result, pageList.Total, "查询成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<List<PlcWriteDbInfoDto>>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<PlcWriteDbInfoDto>> GetPlcWriteDbInfoByIDAsync(long ID)
        {
            try
            {
                var result = await _PlcWriteDbInfoApp.GetPlcWriteDbInfoByIDAsync(ID);
                return ApiBase.Success(result, "获取数据成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<PlcWriteDbInfoDto>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<List<PlcWriteDbDto>>> GetPlcWriteDbInfoAsync()
        {
            try
            {
                var result = await _PlcWriteDbInfoApp.GetPlcWriteDbInfoAsync();
                return ApiBase.Success(result, "获取数据成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<List<PlcWriteDbDto>>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<List<DispatchEquipmentPlanarState>>> GetDispatchEquipmentPlanarStateInfoAsync(List<string> equipmentCodes)
        {
            try
            {
                if (equipmentCodes.Count <= 0)
                    return ApiBase.Error<List<DispatchEquipmentPlanarState>>("未传参数，请检查");

                var result = await _PlcWriteDbInfoApp.GetDispatchEquipmentPlanarStateInfoAsync(equipmentCodes);
                return ApiBase.Success(result, "获取数据成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<List<DispatchEquipmentPlanarState>>(ex.Message);
            }
        }
    }
}
